package com.matt.foundation.utils.export;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.data.category.DefaultCategoryDataset;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Vector;

public class DualaxisChart {
	public DualaxisChart() {
	}

	public static JFreeChart createChart(String barName, String lineName, String[] categories, Object[] barValues, Object[] lineValues) {
//		// 净利润
//		Object netProfit[] = {107.1, 78.6, 85.7, 100.0, 71.4, 85.7};
//		Object payoutRatio[] = {"7.5","5.5","6.0","7.0","5.0","6.0"};
//		String categories[] = {"大动作","精细动作","适应能力","语言","社会行为","交流互动警示行为"};

		Vector<Serie> seriesNetProfit = new Vector<Serie>();
		seriesNetProfit.add(new Serie(barName, barValues));

		Vector<Serie> seriesPayoutRatio = new Vector<Serie>();
		seriesPayoutRatio.add(new Serie(lineName, lineValues));

		//bar chart
		DefaultCategoryDataset datasetNetProfit = ChartUtils.createDefaultCategoryDataset(seriesNetProfit, categories);
		JFreeChart chart = ChartFactory.createBarChart("", "", barName, datasetNetProfit);
		ChartUtils.setAntiAlias(chart);// 抗锯齿
		ChartUtils.setBarRenderer(chart.getCategoryPlot(), false);

		// 设置坐标轴
		ChartUtils.setXAixs(chart.getCategoryPlot());
		ChartUtils.setYAixs(chart.getCategoryPlot());

		// linechart
		CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
		DefaultCategoryDataset datasetPayoutRatio = ChartUtils.createDefaultCategoryDataset(seriesPayoutRatio, categories);
		categoryplot.setDataset(1, datasetPayoutRatio);
		categoryplot.mapDatasetToRangeAxis(1, 1);

		// X轴刻度
		CategoryAxis categoryaxis = categoryplot.getDomainAxis();
		categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);

		// 右侧Y轴
		NumberAxis numberaxis = new NumberAxis(lineName);
		categoryplot.setRangeAxis(1, numberaxis);

		// 隐藏Y刻度
		numberaxis.setAxisLineVisible(false);
		numberaxis.setTickMarksVisible(false);

		// 设置折线图样式
		LineAndShapeRenderer lineRenderer = new LineAndShapeRenderer();
		lineRenderer.setSeriesPaint(0, new Color(255, 185, 1));
		lineRenderer.setDefaultShapesVisible(true);// 数据点绘制形状
		categoryplot.setRenderer(1, lineRenderer);

		categoryplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);// 绘制Z-index, 将折线图在前面
		chart.getLegend().setPosition(RectangleEdge.TOP);//标注在顶部
		chart.getLegend().setFrame(new BlockBorder(Color.WHITE));

		return chart;
	}

	public static BufferedImage createChartBufferedImage(String barName, String lineName, String[] categories, Object[] barValues, Object[] lineValues){
		JFreeChart chart = createChart(barName, lineName,categories, barValues, lineValues);

		//输出文件
		BufferedImage image = new BufferedImage(1300, 500, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = image.createGraphics();

		g2.setRenderingHint(JFreeChart.KEY_SUPPRESS_SHADOW_GENERATION, true);
		Rectangle r = new Rectangle(0, 0, 1300, 500);
		chart.draw(g2, r);

		return image;
	}

}
